﻿using UnityEngine;

namespace winS.Unity
{
    /// <summary>
    /// provides help functions for Color.
    /// </summary>
    public static class ColorUtility
    {
        /// <summary>
        /// scale the <paramref name="color"/>.
        /// </summary>
        public static Color Scale(Color color, float scale)
        {
            //缩放总是以最大的分量为准
            return color * scale / color.maxColorComponent;
        }
        public static string RGBToHEX(Color color)
        {
            byte r = (byte)Mathf.Clamp(Mathf.RoundToInt(color.r * 255f), 0, 255);
            byte g = (byte)Mathf.Clamp(Mathf.RoundToInt(color.g * 255f), 0, 255);
            byte b = (byte)Mathf.Clamp(Mathf.RoundToInt(color.b * 255f), 0, 255);
            byte a = (byte)Mathf.Clamp(Mathf.RoundToInt(color.a * 255f), 0, 255);
            return $"#{r:X2}{g:X2}{b:X2}{a:X2}";
        }
        public static bool TryParseHEXToRGB(string hexColor, out Color color)
        {
            return UnityEngine.ColorUtility.TryParseHtmlString(hexColor, out color);
        }
    }
}